﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CarsWebApplication.businesslogic.constants;
using CarsWebApplication.businesslogic.entities;
using bg.krasi.caroffers;
using System.Web.Configuration;

namespace CarsWebApplication.businesslogic.utils
{
    public class CarWebAppUtility
    {

        public static List<Brand> GetBrands(string brands)
        {
            List<Brand> brandEntities = new List<Brand>();
            brandEntities.Add(new Brand(-1, "-- Select brand --"));
            string[] brandsInfo = brands.Split('\n');
            for (int index = 1; index < brandsInfo.Length; index++)
            {
                string currentBrand = brandsInfo[index];
                if (string.IsNullOrEmpty(currentBrand) == false)
                {
                    Brand brand = new Brand(currentBrand);
                    brandEntities.Add(brand);                
                }
            }
            return brandEntities;
        }

        public static List<Model> GetModels(string models)
        {
            List<Model> modelEntities = new List<Model>();
            modelEntities.Add(new Model(-1, "-- Select model --"));
            string[] modelsInfo = models.Split(CarWebAppConstants.NEW_LINE);
            for (int index = 1; index < modelsInfo.Length; index++)
            {
                string currentModel = modelsInfo[index];
                if (string.IsNullOrEmpty(currentModel) == false)
                {
                    Model model = new Model(currentModel);
                    modelEntities.Add(model);
                }
            }
            return modelEntities;
        }

        public static List<Offer> GetOffers(string offers)
        {
            List<Offer> offerEntities = new List<Offer>();
            string[] offersInfo = offers.Split(CarWebAppConstants.NEW_LINE);
            for (int index = 1; index < offersInfo.Length; index++)
            {
                string currentOffer = offersInfo[index];
                if (string.IsNullOrEmpty(currentOffer) == false)
                {
                    Offer offer = new Offer(currentOffer);
                    offerEntities.Add(offer);
                }
            }
            return offerEntities;
        }

        public static bool IsValidId(string idTxt, out int brandId)
        {
            brandId = CarWebAppConstants.INVALID_ID;
            bool validBrandId = Int32.TryParse(idTxt, out brandId);
            if (validBrandId == true)
            {
                validBrandId = (brandId >= 0);
            }
            return validBrandId;
        }

        public static bool IsValidYear(string yearTxt, out int year)
        {
            year = CarWebAppConstants.INVALID_ID;
            bool validYearId = Int32.TryParse(yearTxt, out year);
            if (validYearId)
            {
                validYearId = (year >= 1900);
            }
            return validYearId;
        }

        public static bool IsValidPrice(string priceTxt, out double price)
        {
            price = 0D;
            bool validPrice = Double.TryParse(priceTxt, out price);
            if (validPrice)
            {
                validPrice = (price > 0D);
            }
            return validPrice;
        }

        /// <summary>
        /// Sets WS URL from web.config
        /// </summary>
        /// <param name="mobileOffersService"></param>
        public static void SetMobileOffersWSUrl(MobileOffersService mobileOffersService)
        {
            string wsUrl = WebConfigurationManager.AppSettings[CarWebAppConstants.MOBILE_OFFERS_WS_URL_CONFIG_KEY];
            mobileOffersService.Url = wsUrl;            
        }
    }
}